import { fileURLToPath, URL } from 'url'
// import topLevelAwait from 'vite-plugin-top-level-await'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import VitePluginBuildApis from './build/bin/vite-plugin-build-apis'

// 自定义组件 name
import vueSetupExtend from 'vite-plugin-vue-setup-extend'

import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import { resolve } from 'path'

import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
// https://vitejs.dev/config/k
export default defineConfig({
  base: '/mgr/',
  build: {
    target: 'esnext'
  },
  css: {
    //css预处理器
    preprocessorOptions: {
      less: {
        javascriptEnabled: true,
        //引入全局变量
        additionalData: '@import "./src/styles/theme-vars.less";',
        modifyVars: {
          'primary-color': '#0083FF'
        }
      }
    }
  },
  optimizeDeps: {
    include: ['dayjs/locale/zh-cn']
  },
  plugins: [
    vue(),
    vueJsx(),
    VitePluginBuildApis(),
    vueSetupExtend(),
    createSvgIconsPlugin({
      // 指定要缓存网站的图标文件夹
      iconDirs: [resolve(process.cwd(), './src/assets/icon/svg')],
      // 执行icon name的格式
      symbolId: 'icon-[name]'
    }),
    //
    Components({
      resolvers: [
        AntDesignVueResolver({
          importStyle: 'less' // 一定要开启这个配置项
        })
      ]
    })

    // topLevelAwait({
    //   // The export name of top-level await promise for each chunk module
    //   promiseExportName: '__tla',
    //   // The function to generate import names of top-level await promise in each chunk module
    //   promiseImportName: (i) => `__tla_${i}`
    // })
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
      '@cms': fileURLToPath(new URL('./src/components/cms', import.meta.url))
    }
  }
})
